#include "Stdafx.h"
#include "RenderTargetView.h"

#include "RenderDevice.h"
#include "Texture2D.h"
#include "Texture.h"

namespace WindGE
{
	RenderTargetView::RenderTargetView()
		: RenderView()
		, rtv_(nullptr)
	{
	}

	RenderTargetView::~RenderTargetView()
	{
		release();
	}

	bool RenderTargetView::init(RenderDevicePtr renderDevice, TexturePtr tex, Texture2DPtr texBuff)
	{
		tex_ = tex;
		tex_buff_ = texBuff;
		
		D3D11_RENDER_TARGET_VIEW_DESC rtvDesc;
		rtvDesc.Format = texBuff->desc().Format;
		rtvDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
		rtvDesc.Texture2D.MipSlice = 0;

		HRESULT hr = renderDevice->device()->CreateRenderTargetView(texBuff->d3dptr(), &rtvDesc, &rtv_);

		if (FAILED(hr)) return false;

		return true;
	}

	void RenderTargetView::release()
	{
		safe_release(rtv_);

		if (tex_) tex_->release();
		if (tex_buff_) tex_buff_->release();
	}

}//end namespace WindGE

